package generator;

import java.io.IOException;

import util.Configuration;
import util.TimeData;
import util.io.FileImporter;
import util.io.FileImporterLap;
import util.io.FileImporterStage;

/**
 * Contains the main-method for the result generator. Creates all objects that
 * are needed.
 * 
 * @author team07
 */
public class Main {

	public TimeData td;

	/**
	 * Main method for running the ResultsGenerator in a prompt. It takes an
	 * arbitrary number of parameters of startfiles, finishfiles and namefiles
	 * 
	 * This method only loads a configuration file and calls the mainActual()
	 * method.
	 * 
	 * Do NOT call this method from a test case.
	 */
	public static void main(String[] args) {
		try {
			Configuration.loadFromXML(Configuration.CONFNAMESINIT);

			if (args.length <= 0) {
				new Gui();
			} else {
				loadAndGenerate(args);
			}

		} catch (IOException e) {
			System.err.println(e.getMessage());
		} catch (IllegalArgumentException e) {
			System.err.println(e.getMessage());
		}

	}
	
	/**
	 * This is the method that does the actual work, loading the files etc
	 * 
	 * Use this method for the acceptance test.
	 * 
	 * @throws IOException
	 *             if the file can not be read
	 */
	public static void loadAndGenerate(String[] args) throws IOException {
		// args are all the data files to be added.
		try {
		FileImporter importer;

			if (Configuration.get(Configuration.COMPETITION_TYPE).equals(
					"stage")) {
				importer = new FileImporterStage();
			} else {
				importer = new FileImporterLap();
			}
			for (String str : args) {
				importer.loadFile(str);
			}
			ResultsGenerator rg = new ResultsGenerator(importer.getData());
			rg.generateResults("results_id.txt", "results_pos.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}